<?php

/**
 * Albireo Kernel
 *
 * @copyright  Copyright (c) 2010 Albireo Solutions
 * @package    Kernel
 */

namespace KernelModule\ModulesModule;

use Nette\Web\Session;

use KernelModule\BasePresenter;
use Albireo\OAuth\AuthClient;
use WebtopModule\SystemAppModule\GalleryModule\PicasaWeb;


/**
 * Kernel modules management presenter.
 *
 * @author     Albireo Solutions
 * @package    Kernel
 */
class PicasaPresenter extends BasePresenter {
    
    public function actionDefault() {
        $session = $this->getSession("picasa")->token;
        if($session === null) {
            \Nette\Debug::dump($session);
            //throw new \Exception();
            $this->redirectUri('https://accounts.google.com/o/oauth2/auth?client_id=377304209821.apps.googleusercontent.com&redirect_uri=http://localhost/AlbireoIS/document_root/kernel/picasa/test&scope=https://picasaweb.google.com/data/&response_type=token');
        } else {
            $this->redirect("authorized");
        }
        //$this->redirectUri('https://www.google.com/accounts/AuthSubRequest?scope=https://picasaweb.google.com/data&session=1&secure=0&next=http://localhost/AlbireoIS/document_root/kernel/picasa/test');
        $this->redirectUri('https://accounts.google.com/o/oauth2/auth?client_id=377304209821.apps.googleusercontent.com&redirect_uri=http://localhost/AlbireoIS/document_root/kernel/picasa/test&scope=https://picasaweb.google.com/data/&response_type=token');
        $callbackUrl = "http://localhost/AlbireoIS/document_root/kernel/picasa/access";
        $scope = "https://picasaweb.google.com/data/";
        
        $oAuthHandler = new AuthClient();
        $requestTokenInfo = $oAuthHandler->getRequestToken($callbackUrl, $scope);
        $this->getSession("oauth")->tokenInfo = $requestTokenInfo;
        
        //$driver = new \Celebrio\FileSystem\PicasaWeb();
        //$driver->saveTokens($accessTokenInfo);
        
        $this->redirectUri($oAuthHandler->authorizationUrl($requestTokenInfo));
        throw new \Exception("The system should never reach this line");
    }
    
    public function actionAccess($oauth_token, $oauth_verifier) {
        $token_secret = $this->getSession("oauth")->tokenInfo["oauth_token_secret"];
        $oAuthHandler = new AuthClient();
        //\Nette\Debug::dump();
        $accessTokenInfo = $oAuthHandler->getAccessToken($oauth_token, $token_secret);
        $this->getSession("oauth")->tokenInfo = $accessTokenInfo;
        
       
        $driver = new \Celebrio\FileSystem\PicasaWeb();
        //$driver->saveTokens($accessTokenInfo);
        
        $this->redirect("authorized");
        throw new \Exception("The system should never reach this line");
    }
    
    public function renderAuthorized($a) {
        /*$this->template->title = "Google - authorized";
        //\Nette\Debug::dump($this->getSession("oauth")->tokenInfo);

        $accessToken = $this->getSession("oauth")->tokenInfo["oauth_token"];
        $accessTokenSecret = $this->getSession("oauth")->tokenInfo["oauth_token_secret"];
        
        
        $url = "https://picasaweb.google.com/data/feed/api/user/default/albumid/5635866697799131761/photoid/5635866696164550178";
        $oAuthHandler = new AuthClient();
        $result = $oAuthHandler->fetch($accessToken, $accessTokenSecret, $url);
        \Nette\Debug::dump($result);
        //$this->template->result = $result;
        //$xml = new \SimpleXMLElement($result);
        $xml = new SimpleXMLIterator($result); 
        $path = $xml->icon;
        
        $this->template->path = $path;
        \Nette\Debug::dump($xml);
        \Nette\Debug::dump($path);*/
        $access_token = $this->getHttpRequest()->getQuery("access_token");
        \Nette\Debug::dump($access_token);
        $this->getSession("picasa")->token = $access_token;
        //$this->getSession("picasa")->token = $access_token;
        $this->template->title = "Test PicasaWeb layout";
        
        $model = new PicasaWeb($access_token);
        $x = $model->test();
//        foreach($x->link as $title) {
//            \Nette\Debug::dump($title);
//        }
        //\Nette\Debug::dump($x);
    }
    
    public function renderTest() {
        
    }
}